package entities;

import util.ChangingModel;

/**
 * Entity Class for Addresses. Adress consists of street, housenumber, postal,
 * city and an identifier.
 * 
 * @author MK
 * 
 */
public class Address extends ChangingModel {

	private String street;
	private int housenumber;
	private int postal;
	private String city;
	private String identifier;

	public Address() {
		super();
	}

	public Address(String street, int housenumber, int postal, String city,
			String identifier) {
		this.street = street;
		this.housenumber = housenumber;
		this.postal = postal;
		this.city = city;
		this.identifier = identifier;
	}

	public Address(Address address) {
		this(address.street, address.housenumber, address.postal, address.city,
				address.identifier);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + housenumber;
		result = prime * result + postal;
		result = prime * result + ((street == null) ? 0 : street.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Address other = (Address) obj;
		if (housenumber != other.housenumber)
			return false;
		if (postal != other.postal)
			return false;
		if (street == null) {
			if (other.street != null)
				return false;
		} else if (!street.equals(other.street))
			return false;
		return true;
	}

	@Override
	public String toString() {
		if (identifier == null) {
			return street + " " + housenumber + ", " + postal + " " + city;
		} else {
			return identifier;
		}
	}

	public int compareTo(Address address) {

		if (this.postal != address.postal) {
			return this.postal - address.postal;
		} else {
			if (!this.street.equals(address.street)) {
				return this.street.compareTo(address.street);
			} else {
				return this.housenumber - address.housenumber;
			}
		}
	}

	public String getStreet() {
		return street;
	}

	public int getHousenumber() {
		return housenumber;
	}

	public int getPostal() {
		return postal;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getIdentifier() {
		return identifier;
	}

	public void setIdentifier(String identifier) {
		this.identifier = identifier;
	}
}